<?php
    require('includes/expresscheckout.php');
    require('conn.cfg.php');
    require('functions.php');
    
	$isComplete = false; //determine payment status whether it was completed or failed
	if(isset($_SESSION['username']) && isset($_REQUEST['ack'])){
            if($_REQUEST['ack'] == 'ok'){
		
				$_SESSION["token"] = $_REQUEST["token"];
				$_SESSION["payerid"] = $_REQUEST["PayerID"];
				
				// Set request-specific fields.    
				$nvpStr = "&TOKEN=" . $_SESSION["token"];
				// Execute the API operation; see the PPHttpPost function above.
				$httpParsedResponseAr = PPHttpPost('GetExpressCheckoutDetails', $nvpStr);

				if("SUCCESS" == strtoupper($httpParsedResponseAr["ACK"]) || "SUCCESSWITHWARNING" == strtoupper($httpParsedResponseAr["ACK"])) {
					// get checkout details & complete transaction                        
					// get values from responses messages;
					$token      = urldecode($httpParsedResponseAr["TOKEN"]);
					$payerid    = urldecode($httpParsedResponseAr["PAYERID"]);
					$Amount     = urldecode($httpParsedResponseAr["PAYMENTREQUEST_0_AMT"]);
					$currencyID = urldecode($httpParsedResponseAr['PAYMENTREQUEST_0_CURRENCYCODE']);
					
					dump('GetExpressCheckoutDetails sucessfull: token ' . $token . '/payerid ' .$payerid);

					//insert pending order
					$orderid = savePendingOrder($httpParsedResponseAr);
					dump("savePendingOrder $orderid sucessfull");
					
					saveOrderDetails($orderid);
					dump('saveOrderDetails sucessfull');
					
					// Set request-specific fields. 
					$nvpStr = "&TOKEN=$token&PAYERID=$payerid&PAYMENTREQUEST_0_AMT=$Amount&PAYMENTREQUEST_0_CURRENCYCODE=$currencyID";
					// COMPLETING THE TRANSACTION
					$httpParsedResponsePaid = PPHttpPost('DoExpressCheckoutPayment', $nvpStr);

					if("SUCCESS" == strtoupper($httpParsedResponsePaid["ACK"]) || "SUCCESSWITHWARNING" == strtoupper($httpParsedResponsePaid["ACK"])){
						dump('DoExpressCheckoutPayment sucessfull');
						$txnum = urldecode($httpParsedResponsePaid["PAYMENTINFO_0_TRANSACTIONID"]);
						updateOrder($orderid, $txnum);
						$msg = 'Transaction was completed RefId= ' . $txnum;
						
						//clear shopping cart & user information
						unset($_SESSION['cart']);
						unset($_SESSION['isAuthenticated']);
						unset($_SESSION['username']);
						$isComplete = true;    
					}else{
						$msg = 'Your payment request was failed. ' . print_r($httpParsedResponsePaid, true);
					}                
						
				} else  {
						$msg = 'Cannot getting your details payment. ' . print_r($httpParsedResponsePaid, true);
				}                               
            }else if($_REQUEST['ack'] == 'ko'){
                $msg = "Your payment request was cancelled."; 
            }else{            
                $msg = "Your payment request cannot process at the moment. Please try again later!";
            }       
        }else {
		$msg = "Your payment request cannot process at the moment. Please try again later!";    
	}	
    
    /**    
     * @param type $reponses
     * @param type $keyid
     * @return string 
     *  get value from response array
     */
    function getValueByKey($reponses,$keyid){
        $ret = "";
        if (is_array($reponses)){
            foreach($reponses as $key => $value){
                if  (strtoupper($key) == strtoupper($keyid)) {
                    $ret = urldecode($value);
                    break;
                }
            }
        }
        return $ret;
    }
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>X2PERFORMANCE :: Shipping</title>
<link rel="stylesheet" href="assets/css/reset.css" />
<link rel="stylesheet" href="assets/css/text.css" />
<link rel="stylesheet" href="assets/css/960.css" />
<link rel="stylesheet" href="assets/css/style.css" />
<link rel="SHORTCUT ICON" href="favicon.ico"/>
<script type="text/javascript" src="assets/js/jquery-1.5.2.min.js"></script>
<script type="text/javascript" src="assets/js/kickstart.js"></script>
<script type="text/javascript" src="assets/js/script00.js"></script>
<script type="text/javascript" src="assets/js/skimlink.js"></script>
<script type="text/javascript" src="assets/js/jquery.cycle.js"></script>
<script type="text/javascript" src="assets/js/slideshow.js"></script>
<link rel="stylesheet" href="assets/css/coda-slider.css" type="text/css" media="screen" title="no title" charset="utf-8">
<script src="assets/js/jquery_002.js" type="text/javascript"></script>
<script src="assets/js/jquery_003.js" type="text/javascript" charset="utf-8"></script>
<script src="assets/js/jquery.js" type="text/javascript" charset="utf-8"></script>
<!--<script src="assets/js/coda-slider.js" type="text/javascript" charset="utf-8"></script>-->

<!--[if IE 7]>
	<style>
    #bottle{ margin-left:-335px;}
    #nav {height:45px;}
    #nav li { display: inline;margin-bottom:8px;}
    </style>
<![endif]-->
<!-- CuFon: Enables smooth pretty custom font rendering. 100% SEO friendly. To disable, remove this section -->
<script type="text/javascript" src="assets/js/cufon-yui.js"></script>
<script type="text/javascript" src="assets/js/PT_Sans_400-PT_Sans_700-PT_Sans_italic_400-PT_Sans_italic_700.font.js"></script>
<script type="text/javascript" src="assets/js/cuf_run.js"></script>
<!-- CuFon ends -->
</head>
<body>
    
<div  id="header">
  <div class="container_16">
    <noscript>
    <div class="alert"> In order to be able to view this website, Javascript needs to be active in your web browser. </div>
    </noscript>
    <div class="grid_8"> <a id="logo" href="index.html" title="X2 Performance"></a> </div>
    <div class="grid_3"> <a id="nsf" href="http://nsfsport.com/listings/certified_detail.asp?id=707999" title="NSF Certified for Sport" target="_blank"></a> </div>
    <div class="grid_5" id="topnavigation">
      <ul id="topnav">
        <!--<li><a href="">Login</a></li>
         <li><a>|</a></li>
        <li><a href="">Register</a></li>-->
        <li><span class="viewcart"></span><a href="x2cart.php">View cart</a></li>
      </ul>
    </div>
    <div class="clear"></div>
    <div class="grid_16" id="navigation">
      <ul id="nav">
        <li><a href="index.html" class="home">Home</a></li>
        <li><a href="about_us.html" class="aboutcompany">About Us</a></li>
        <li class="current_page_item current_page"><a href="services.html" class="x2performanceactive">X<sup>2</sup>PERFORMANCE</a></li>
        <li><a href="sport.html" class="sportcertified">sportcertified</a></li>
        <li><a href="contact_us.html" class="contact">Contact Us</a></li>
        <li><a href="blog.html" class="blog">Blog</a></li>
      </ul>
      <!--ul class="order">
        <li><a href="order_now.html" class="ordernow">Order now</a></li>
      </ul-->
    </div>
  </div>
</div>
<div class="container_16">
  <div class="grid_16 innerheader">
    <div class="innerheaderbg"></div>
    <h2>Confirm order</h2>
  </div>
  <div class="clear"></div>  
  <div id="productGrid">        
    <div id="contentProductDetail">
        <h3><?php if (isset($msg)){ echo $msg;}?></h3>      
<?php
    if($isComplete){
?>          
        <table class="viewCart tabular grid" width="450">
        <tbody>
            <tr>
                <th colspan="2"><h4>Shipping information</h4></th>                
            </tr>         
            <tr>
                <td>First Name</td>
                <td><?= getValueByKey($httpParsedResponseAr, 'FIRSTNAME')?></td>
            </tr>
            <tr>
                <td>Last Name</td>
                <td><?= getValueByKey($httpParsedResponseAr, 'LASTNAME')?></td>
            </tr>
            <tr>
                <td>Ship to name</td>
                <td><?= getValueByKey($httpParsedResponseAr, 'SHIPTONAME')?></td>
            </tr>
            <tr>
                <td>Ship to street</td>
                <td><?= getValueByKey($httpParsedResponseAr, 'SHIPTOSTREET')?></td>
            </tr>
            <tr>
                <td>Ship to city</td>
                <td><?= getValueByKey($httpParsedResponseAr, 'SHIPTOCITY')?></td>
            </tr>
            <tr>
                <td>Ship to state</td>
                <td><?= getValueByKey($httpParsedResponseAr, 'SHIPTOSTATE')?></td>
            </tr>
            <tr>
                <td>Ship to country code</td>
                <td><?= getValueByKey($httpParsedResponseAr, 'SHIPTOCOUNTRYCODE')?></td>
            </tr>
            <tr>
                <td>Ship to country name</td>
                <td><?= getValueByKey($httpParsedResponseAr, 'SHIPTOCOUNTRYNAME')?></td>
            </tr>
            <tr>
                <td>Amount</td>
                <td><?= getValueByKey($httpParsedResponseAr, 'AMT')?></td>
            </tr>
            <tr>
                <td>Product Name</td>
                <td><?= getValueByKey($httpParsedResponseAr, 'L_NAME0')?></td>
            </tr>
             <tr>
                <td>Quantity</td>
                <td><?= getValueByKey($httpParsedResponseAr, 'L_QTY0')?></td>
            </tr>
        </tbody>
	</table>    
<?php 
    }
?>        
    </div>                 	
  </div>

</div>
<br clear="all">
<div id="footer">
  <div class="container_16">
    <div id="footersectionone">
      <div class="grid_1">
        <h3><a href="index.html">Home</a></h3>
      </div>
      <div class="grid_3 footermenucontainer">
        <h3><a href="about_us.html">About Us</a></h3>
        <ul class="mainlinks">
          <li><a href="about_us.html#ceomessage">CEO Message</a></li>
        </ul>
      </div>
      <div class="grid_3 footermenucontainer">
        <h3><a href="services.html">X<sup>2</sup>PERFORMANCE</a></h3>
        <ul class="mainlinks">
          <li><a href="services.html#science">Science</a></li>
          <li><a href="supplymentfacts.html">Supplement facts</a></li>
          <li><a href="instruction.html">Instructions</a></li>
          <li><a href="pricing.html">Pricing</a></li>
          <li><a href="testimonial.php">Testimonials</a></li>
        </ul>
      </div>
      
      <div class="grid_2">
        <h3><a href="contact_us.html">Contact Us</a></h3>
        <ul class="mainlinks">
          <li><a href="privacy.html">Privacy Policy</a></li>
          <li><a href="terms.html">Terms</a></li>
        </ul>
      </div>
      <div class="grid_2">
        <h3><a href="sport.html">Sport certified</a></h3>
      </div>
      <div class="grid_1">
        <h3><a href="blog.html">Blog</a></h3>
      </div>
      <div class="grid_5" id="socialicons">
        <h3>CONNECT WITH US</h3>
        <a id="twitter" href="http://twitter.com/#!/x2performance" title="Twitter" target="_blank"></a> <a id="facebook" href="http://www.facebook.com/X2PERFORMANCE" title="Facebook" target="_blank"></a> <a id="youtube" href="http://www.youtube.com/user/TEAMX2PERFORMANCE/featured" title="You Tube" target="_blank"></a> <a id="googleplus" href="https://plus.google.com/109519688499936342953/posts" title="Google Plus" target="_blank"></a> </div>
      <div class="clear"></div>
    </div>
    <div id="footersectiontwo"> <span id="footerlogo"></span>
      <h3><span>MAKE IT HAPPEN <sup>®</sup></span><br/>
        PROUDLY MADE IN THE USA &copy; 2012 Advanced Bio Development LLC</h3>
      <span id="footerlogo2"></span> </div>
  </div>
 
</div>
<script type="text/javascript" src="assets/js/menu.js"></script>
</body>
</html>